package cn.gzsf.springboot08.controller;


import cn.gzsf.springboot08.entity.User;
import cn.gzsf.springboot08.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/check")
    public int check(String username){
        User u = userMapper.selectByUsername(username);
        return u==null?1:2; //1代表可用，2代表不可用
    }

    @RequestMapping("/reg")
    public int reg(User user){
        User u = userMapper.selectByUsername(user.getUsername());
        if(u!=null){
            return 2;//用户名被占用，不准注册
        }
        userMapper.insert(user);
        return 1;//用户注册成功
    }
    @RequestMapping("/login")
    public int login(User user){
        User u = userMapper.selectByUsername(user.getUsername());
        if(u==null){
            return 2;//用户名不存在
        }
        //程序能走到这里，说明查到用户了
        if(u.getPassword().equals(user.getPassword())){
            return 1;//登录成功
        }
        //程序能走到这里，说明查到用户,但是密码不对
        return 3;//密码错误
    }
}
